package be.fomp.jeve.application.data.standard.skilltree;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jdom.Element;

import be.fomp.jeve.application.data.standard.SkillTree.Group;

final class GroupImpl implements Group{
	static final long serialVersionUID = 1L;
	
	private int groupId;
	private String groupName;
	
	private final Map<Integer, Skill> skills = new HashMap<Integer, Skill>();
	
	@SuppressWarnings("unchecked")
	GroupImpl(Integer groupId, Element groupElement){
		this.groupId = groupId;
		this.groupName = groupElement.getAttributeValue("groupName");
		
		List<Element> resultSet = groupElement.getChild("rowset").getChildren();
		for(Element skillElement: resultSet) {
			Skill skill = new SkillImpl(skillElement);
			Integer skillId = skill.getTypeId();
			
			this.skills.put(skillId, skill);
		}
	}
	
	public int getGroupId() {return this.groupId;}
	public String getGroupName() {return this.groupName;}
	
	public Collection<Skill> getSkills(){
		return this.skills.values();
	}
	
	public Skill getSkillByType(Integer skillType) {
		return this.skills.get(skillType);
	}
	
	public Skill getSkillByName(String typeName) {
		Skill returnValue = null;
		
		for(Skill s: getSkills()){
			if(groupName.equals(s.getTypeName())){
				returnValue = s;
				break;
			}
		}
		
		return returnValue;
	}
}
